const path = require('path');
const { app } = require('electron');
const fse = require('fs-extra')
const process = require('process')
const { envConst } = require('../constans/envConstans')

exports.copyToTemp = function (basePath) {
    process.noAsar = true
    const dst = fse.mkdtempSync(path.join(exports.getUserCachePath(), "basePlugin"))
    fse.copySync(basePath, dst)
    return dst
}


exports.findFolder = function (thePath) {
    let files = fse.readdirSync(thePath);
    let result = [];
    files.forEach((val, index) => {
        let fPath = path.join(thePath, val);
        let stats = fse.statSync(thePath);
        // 忽略以.开头的隐藏文件夹
        if (stats.isDirectory() & !val.startsWith(".")) result.push(fPath);
    });
    return result

}

exports.getResoucePath = function (thPath) {
    if (envConst.dev()) {
        return path.join(__dirname, '../../statics', thPath)
    } else {
        return path.join(__dirname, '../../statics', thPath)
        // todo 生产环境的资源地址
        return path.join(process.resourcesPath, thPath)
    }
}


exports.getMainPreloadJsPath = function () {
    return path.join(__dirname, '../../preload/preload.js')
}


exports.rmdir = function (dir) {
    fse.removeSync(dir)
}

exports.rmFile = function (file) {
    try {
        fse.removeSync(file)
    } catch (e) { }
}


exports.mkdir = function (dir) {
    fse.mkdirs(dir, () => { });
}

exports.getUserCachePath = function () {
    return app.getPath("cache")
}


exports.getUserDataPath = function () {
    return app.getPath("userData")
}

exports.pluginResPathWarper = function (pluginId, resPath, isFileProtocol = true) {
    const header = isFileProtocol ? "file:///" : ""
    return path.join(header, exports.getPluginPath(), pluginId, resPath)
}

exports.getPluginResourcePath = function (pluginId) {
    const resourcePath = path.join(exports.getUserDataPath(), 'pluginResource')
    if (!fse.existsSync(resourcePath)) {
        fse.mkdirSync(resourcePath)
    }

    const resourcePathById = path.join(resourcePath, pluginId)
    if (!fse.existsSync(resourcePathById)) {
        fse.mkdirSync(resourcePathById)
    }

    return resourcePathById
}

exports.getPluginPath = function () {
    return path.join(exports.getUserDataPath(), 'plugin')
}


exports.getPluginResoucePathByPluginId = function (pluginId) {
    return path.join(exports.getUserDataPath(), 'plugin', pluginId)
}

exports.getLevelDbPath = function () {
    return path.join(exports.getUserDataPath(), "level_db_data/data")
}

exports.getPluginAsarPathByPluginId = function (pluginId, isFileProtocol = true) {
    const header = isFileProtocol ? "file:///" : ""
    return path.join(header, exports.getPluginPath(pluginId), pluginId + '.asar')
}

exports.getPluginPathByPluginId = function (pluginId, isFileProtocol = false) {
    return path.join(exports.getPluginAsarPathByPluginId(pluginId, isFileProtocol))
}

exports.getPluginPreloadPathByPluginId = function (pluginId, preloadPath, isFileProtocol = false) {
    return path.join(exports.getPluginPathByPluginId(pluginId, isFileProtocol), preloadPath)
}


exports.getPluginInfoPathByPluginId = function (pluginId, isFileProtocol = false) {
    return path.join(exports.getPluginAsarPathByPluginId(pluginId, isFileProtocol), "plugin.json")
}

exports.getSqliteDbPathByName = function (fileName) {
    const thePath = path.join(exports.getUserDataPath(), "sqlite")
    exports.mkdir(thePath)
    return path.join(thePath, fileName + ".db")
}

exports.openFile = function (path) {
    try {
        return fse.readFileSync(path, 'utf-8')
    } catch (e) {
        return ""
    }
}

exports.writeFile = function (path, content) {
    return fse.writeFileSync(path, content)
}

exports.openJson = function (path) {
    const data = exports.openFile(path)
    try {
        return JSON.parse(data)
    } catch (e) {
        console.error("parse json file error. path=" + path + ",error=" + e)
    }
}